package com.buddy.design.pattern._10observer;

import com.buddy.design.pattern._10observer.advice.Gper;
import com.buddy.design.pattern._10observer.advice.Question;
import com.buddy.design.pattern._10observer.advice.Teacher;
import com.buddy.design.pattern._10observer.event.mouse.Mouse;
import com.buddy.design.pattern._10observer.event.mouse.MouseEventCallback;
import com.buddy.design.pattern._10observer.event.mouse.MouseEventType;
import com.buddy.design.pattern._10observer.guava.GuavaEvent;
import com.google.common.eventbus.EventBus;
import org.junit.Test;

public class ObserverPattern {

    /*
     *  观察者模式：
     *      定义了对象之间的一对多依赖，让多个观察者对象同时监听一个主体对象，当主体对象发生变化时
     *      它所有依赖者(观察者)都会收到通知并更新
     *
     *  属于行为模式
     *
     *  发布订阅模式
     *
     *  ContextLoaderListener
     *
     */

    @Test
    public void gperTest(){
        Gper gper = Gper.getInstance();
        Teacher tom = new Teacher("Tom");

        Question question = new Question();
        question.setUserName("Buddy");
        question.setContent("观察者模式适用于哪些场景？");

        gper.addObserver(tom);
        gper.publishQuestion(question);
    }

    @Test
    public void mouseTest(){
        MouseEventCallback callback = new MouseEventCallback();

        Mouse mouse = new Mouse();
        mouse.addListener(MouseEventType.ON_CLICK,callback);
        mouse.click();
    }

    @Test
    public void guavaTest(){
        GuavaEvent guavaEvent = new GuavaEvent();

        //消息总线
        EventBus eventBus = new EventBus();
        eventBus.register(guavaEvent);
        eventBus.post("Tom");
    }
}
